package com.supersit.greensproject.AsyncTask;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.supersit.greensproject.DB.XUtilsDbManager;
import com.supersit.greensproject.R;
import com.supersit.greensproject.entity.DataUpload;
import com.supersit.greensproject.resources.API;
import com.supersit.greensproject.utils.HttpXUtils;
import com.supersit.greensproject.utils.L;
import com.supersit.greensproject.utils.MyCallBack;
import com.supersit.greensproject.utils.T;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.DbManager;
import org.xutils.common.Callback;
import org.xutils.ex.DbException;

import java.util.HashMap;
import java.util.Map;

/**
 * 网络触发后台把未上传的上报数据进行上传
 * Created by Administrator on 2016/8/27 0027.
 */
public class UploadDataBatchUpload {
    private Context context;
    private DbManager db;
    private final int UPLOAD_SUCCESS = 0x10;
    private final int UPLOAD_FAIL = 0x11;
    private final int SERVICE_ERROR = 0x12;

    public UploadDataBatchUpload(Context context) {
        this.context = context;
        this.db = XUtilsDbManager.getInstance();
    }

    /**
     * 数据上报
     */
    private void dataUpload(final DataUpload dataUpload) {
        String url = API.UploadAndUpdateUploadDada;
        Map<String, Object> map = new HashMap<>();
        Gson gson = new Gson();
        String str = gson.toJson(dataUpload);
        map.put("dataReportItem", str);
        Callback.Cancelable cancelable = HttpXUtils.Post(url, map, new MyCallBack<String>() {
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                L.e("Request Error:" + ex.getMessage());

                Message message = new Message();
                message.what = SERVICE_ERROR;
                handler.sendMessage(message);
            }

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);

                L.d("Request result:" + result);
                try {
                    JSONObject json = new JSONObject(result);
                    int isflog = json.optInt("isflog");//1成功  0失败
                    if (isflog == 1) {
                        int uploadDataId = json.optInt("id");
                        long timestamp = json.optLong("timestamp");
                        dataUpload.setUploadDataId(uploadDataId);
                        dataUpload.setTimeStamp(timestamp);
                        dataUpload.setIsUpload(0);//0已上传 1未上传
                        dataUpload.setOperationsTatus(-1);
                        db.update(dataUpload);
                        Message message = new Message();
                        message.what = UPLOAD_SUCCESS;
                        handler.sendMessage(message);
                    } else {
                        Message message = new Message();
                        message.what = UPLOAD_FAIL;
                        handler.sendMessage(message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Message message = new Message();
                    message.what = SERVICE_ERROR;
                    handler.sendMessage(message);
                } catch (DbException e) {
                    Message message = new Message();
                    message.what = SERVICE_ERROR;
                    handler.sendMessage(message);
                    e.printStackTrace();
                }
            }
        });

        System.out.print(str);
        L.d(str);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UPLOAD_SUCCESS:
                    T.showShort(context, R.string.save_ok);
                    break;
                case UPLOAD_FAIL:
                    T.showShort(context, R.string.save_fail);
                    break;
                case SERVICE_ERROR:
                    T.showShort(context, R.string.service_error);
                    break;
            }
        }
    };

}
